package cn.bupt.adt.controller;

import cn.bupt.adt.constant.Constants;
import cn.bupt.adt.entity.autogenerate.InspectView;
import cn.bupt.adt.entity.autogenerate.InspectionItem;
import cn.bupt.adt.model.InspectItemsModel;
import cn.bupt.adt.model.InspectLevelModel;
import cn.bupt.adt.model.inspectSearchModel;
import cn.bupt.adt.service.IInspectionItemsService;
import cn.bupt.adt.service.IinspectOrderService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/InspectItemsController")
public class InspectItemsController {

    @Autowired
    IinspectOrderService inspectOrderService;

    @Autowired
    IInspectionItemsService inspectionItemsService;

    @RequestMapping("/index/{pageNum}")
    public ModelAndView InspectIndex(@PathVariable("pageNum") int pageNum, HttpServletRequest request, inspectSearchModel inspectSearchModel) {
        ModelAndView mv = new ModelAndView(Constants.INSPECTION_INDEX);
        Page<?> page = PageHelper.startPage(pageNum, Constants.PAGESIZE_LARGE, "id desc");
        List<InspectItemsModel> inspects = null;
        List<InspectView> inspectViews = null;
        if (inspectSearchModel.getSearchway() == null) {
            inspectViews = inspectOrderService.getAllInspectItems();
            mv.addObject("inspects", inspectViews);
        } else {
            mv.addObject("searchMethod", inspectSearchModel.getSearchway());
            mv.addObject("content", inspectSearchModel.getContent());
            mv.addObject("inspects", inspects);
        }
        mv.addObject("currentPage", page.getPageNum());
        mv.addObject("allPages", page.getPages());

        return mv;
    }

    //查看订单详情
    @RequestMapping("/detail/{id}")
    public ModelAndView tackedAndODetail(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView(Constants.INSPECTION_DETAIL);
        InspectView inspects = inspectOrderService.getInspectViewById(id);
        mv.addObject("inspects", inspects);
        return mv;
    }

    @RequestMapping("/addinspects")
    public ModelAndView addInstitution() {
        ModelAndView mv = new ModelAndView(Constants.ADD_INSPECTION);
        List<InspectLevelModel> firstLevelList = inspectOrderService.getLevelInspectionItem();
        mv.addObject("firstLevelList", firstLevelList);
        return mv;
    }

    @RequestMapping("/addinspectsPost")
    public synchronized ModelAndView addInstitutionPost(InspectionItem ins, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("forward:/InspectItemsController/index/1");
        String oper_message = null;
        if (inspectionItemsService.addInspection(ins, request))
            oper_message = "添加检查项目成功！";
        else
            oper_message = "添加检查项目失败！";
        mv.addObject("oper_message", oper_message);
        return mv;
    }

    @RequestMapping("/deleteInspect/{id}")
    public ModelAndView deleteIns(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView("forward:/InspectItemsController/index/1");
        String oper_message = null;
        if (inspectionItemsService.deleteInspectById(id))
            oper_message = "删除检查项目成功！";
        else
            oper_message = "删除检查项失败！";
        mv.addObject("oper_message", oper_message);
        return mv;
    }

    @RequestMapping("/modifyIns")
    public ModelAndView modifyIns(HttpServletRequest request, InspectionItem ins) {
        ModelAndView mv = new ModelAndView("forward:/InspectItemsController/detail/" + ins.getId());
        String oper_message = null;
        if (inspectionItemsService.modifyInspectItem(ins, request))
            oper_message = "保存成功！";
        else
            oper_message = "保存失败！";
        mv.addObject("oper_message", oper_message);
        return mv;
    }
}
